/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.vijayan.profile.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.vijayan.profile.NoSuchMyPhotoException;
import com.vijayan.profile.model.MyPhoto;
import com.vijayan.profile.model.impl.MyPhotoImpl;
import com.vijayan.profile.model.impl.MyPhotoModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the my photo service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link MyPhotoUtil} to access the my photo persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author vijayan
 * @see MyPhotoPersistence
 * @see MyPhotoUtil
 * @generated
 */
public class MyPhotoPersistenceImpl extends BasePersistenceImpl<MyPhoto>
	implements MyPhotoPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = MyPhotoImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the my photo in the entity cache if it is enabled.
	 *
	 * @param myPhoto the my photo to cache
	 */
	public void cacheResult(MyPhoto myPhoto) {
		EntityCacheUtil.putResult(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoImpl.class, myPhoto.getPrimaryKey(), myPhoto);
	}

	/**
	 * Caches the my photos in the entity cache if it is enabled.
	 *
	 * @param myPhotos the my photos to cache
	 */
	public void cacheResult(List<MyPhoto> myPhotos) {
		for (MyPhoto myPhoto : myPhotos) {
			if (EntityCacheUtil.getResult(
						MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
						MyPhotoImpl.class, myPhoto.getPrimaryKey(), this) == null) {
				cacheResult(myPhoto);
			}
		}
	}

	/**
	 * Clears the cache for all my photos.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(MyPhotoImpl.class.getName());
		EntityCacheUtil.clearCache(MyPhotoImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the my photo.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(MyPhoto myPhoto) {
		EntityCacheUtil.removeResult(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoImpl.class, myPhoto.getPrimaryKey());
	}

	/**
	 * Creates a new my photo with the primary key. Does not add the my photo to the database.
	 *
	 * @param photoId the primary key for the new my photo
	 * @return the new my photo
	 */
	public MyPhoto create(long photoId) {
		MyPhoto myPhoto = new MyPhotoImpl();

		myPhoto.setNew(true);
		myPhoto.setPrimaryKey(photoId);

		return myPhoto;
	}

	/**
	 * Removes the my photo with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the my photo to remove
	 * @return the my photo that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the my photo with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param photoId the primary key of the my photo to remove
	 * @return the my photo that was removed
	 * @throws com.vijayan.profile.NoSuchMyPhotoException if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto remove(long photoId)
		throws NoSuchMyPhotoException, SystemException {
		Session session = null;

		try {
			session = openSession();

			MyPhoto myPhoto = (MyPhoto)session.get(MyPhotoImpl.class,
					new Long(photoId));

			if (myPhoto == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + photoId);
				}

				throw new NoSuchMyPhotoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					photoId);
			}

			return remove(myPhoto);
		}
		catch (NoSuchMyPhotoException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected MyPhoto removeImpl(MyPhoto myPhoto) throws SystemException {
		myPhoto = toUnwrappedModel(myPhoto);

		Session session = null;

		try {
			session = openSession();

			if (myPhoto.isCachedModel() || BatchSessionUtil.isEnabled()) {
				Object staleObject = session.get(MyPhotoImpl.class,
						myPhoto.getPrimaryKeyObj());

				if (staleObject != null) {
					session.evict(staleObject);
				}
			}

			session.delete(myPhoto);

			session.flush();
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoImpl.class, myPhoto.getPrimaryKey());

		return myPhoto;
	}

	public MyPhoto updateImpl(com.vijayan.profile.model.MyPhoto myPhoto,
		boolean merge) throws SystemException {
		myPhoto = toUnwrappedModel(myPhoto);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, myPhoto, merge);

			myPhoto.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
			MyPhotoImpl.class, myPhoto.getPrimaryKey(), myPhoto);

		return myPhoto;
	}

	protected MyPhoto toUnwrappedModel(MyPhoto myPhoto) {
		if (myPhoto instanceof MyPhotoImpl) {
			return myPhoto;
		}

		MyPhotoImpl myPhotoImpl = new MyPhotoImpl();

		myPhotoImpl.setNew(myPhoto.isNew());
		myPhotoImpl.setPrimaryKey(myPhoto.getPrimaryKey());

		myPhotoImpl.setPhotoId(myPhoto.getPhotoId());
		myPhotoImpl.setProfileId(myPhoto.getProfileId());
		myPhotoImpl.setContent(myPhoto.getContent());
		myPhotoImpl.setWidth(myPhoto.getWidth());
		myPhotoImpl.setHeight(myPhoto.getHeight());
		myPhotoImpl.setType(myPhoto.getType());
		myPhotoImpl.setThumbnail(myPhoto.isThumbnail());

		return myPhotoImpl;
	}

	/**
	 * Finds the my photo with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the my photo to find
	 * @return the my photo
	 * @throws com.liferay.portal.NoSuchModelException if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the my photo with the primary key or throws a {@link com.vijayan.profile.NoSuchMyPhotoException} if it could not be found.
	 *
	 * @param photoId the primary key of the my photo to find
	 * @return the my photo
	 * @throws com.vijayan.profile.NoSuchMyPhotoException if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto findByPrimaryKey(long photoId)
		throws NoSuchMyPhotoException, SystemException {
		MyPhoto myPhoto = fetchByPrimaryKey(photoId);

		if (myPhoto == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + photoId);
			}

			throw new NoSuchMyPhotoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				photoId);
		}

		return myPhoto;
	}

	/**
	 * Finds the my photo with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the my photo to find
	 * @return the my photo, or <code>null</code> if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the my photo with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param photoId the primary key of the my photo to find
	 * @return the my photo, or <code>null</code> if a my photo with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public MyPhoto fetchByPrimaryKey(long photoId) throws SystemException {
		MyPhoto myPhoto = (MyPhoto)EntityCacheUtil.getResult(MyPhotoModelImpl.ENTITY_CACHE_ENABLED,
				MyPhotoImpl.class, photoId, this);

		if (myPhoto == null) {
			Session session = null;

			try {
				session = openSession();

				myPhoto = (MyPhoto)session.get(MyPhotoImpl.class,
						new Long(photoId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (myPhoto != null) {
					cacheResult(myPhoto);
				}

				closeSession(session);
			}
		}

		return myPhoto;
	}

	/**
	 * Finds all the my photos.
	 *
	 * @return the my photos
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyPhoto> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the my photos.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of my photos to return
	 * @param end the upper bound of the range of my photos to return (not inclusive)
	 * @return the range of my photos
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyPhoto> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the my photos.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of my photos to return
	 * @param end the upper bound of the range of my photos to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of my photos
	 * @throws SystemException if a system exception occurred
	 */
	public List<MyPhoto> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<MyPhoto> list = (List<MyPhoto>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;
				String sql = null;

				if (orderByComparator != null) {
					query = new StringBundler(2 +
							(orderByComparator.getOrderByFields().length * 3));

					query.append(_SQL_SELECT_MYPHOTO);

					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);

					sql = query.toString();
				}
				else {
					sql = _SQL_SELECT_MYPHOTO;
				}

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<MyPhoto>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<MyPhoto>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<MyPhoto>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the my photos from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (MyPhoto myPhoto : findAll()) {
			remove(myPhoto);
		}
	}

	/**
	 * Counts all the my photos.
	 *
	 * @return the number of my photos
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_MYPHOTO);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the my photo persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.vijayan.profile.model.MyPhoto")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<MyPhoto>> listenersList = new ArrayList<ModelListener<MyPhoto>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<MyPhoto>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	@BeanReference(type = ProfilePersistence.class)
	protected ProfilePersistence profilePersistence;
	@BeanReference(type = MyExpectationPersistence.class)
	protected MyExpectationPersistence myExpectationPersistence;
	@BeanReference(type = FamilyMemberPersistence.class)
	protected FamilyMemberPersistence familyMemberPersistence;
	@BeanReference(type = HoroscopePersistence.class)
	protected HoroscopePersistence horoscopePersistence;
	@BeanReference(type = MyPhotoPersistence.class)
	protected MyPhotoPersistence myPhotoPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_MYPHOTO = "SELECT myPhoto FROM MyPhoto myPhoto";
	private static final String _SQL_COUNT_MYPHOTO = "SELECT COUNT(myPhoto) FROM MyPhoto myPhoto";
	private static final String _ORDER_BY_ENTITY_ALIAS = "myPhoto.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MyPhoto exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(MyPhotoPersistenceImpl.class);
}